<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>大事件-后台登录页</title>
    <link rel="stylesheet" type="text/css" href="css/reset.css" />
    <link rel="stylesheet" type="text/css" href="css/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <!-- 引入bootstrap的css文件 -->
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <!-- 引入jq文件 -->
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入bootstrap的js文件 -->
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  </head>

  <body>
    <div class="main_wrap">
      <div class="header">
        <a href="#" class="logo"><img src="images/logo.png" alt=""/></a>
        <div class="copyright">
          CopyRight © 2019 深圳百秀信息技术股份有限公司<br />All Rights Reserved
        </div>
      </div>

      <div class="login_form_con">
        <div class="login_title"></div>
        <form class="login_form">
          <i class="iconfont icon-user"></i>
          <i class="iconfont icon-key"></i>
          <input type="text" class="input_txt" placeholder="邮箱/手机号" />
          <input type="password" class="input_pass" placeholder="密码" />
          <input type="submit" class="input_sub" value="登 录" />
        </form>
      </div>
    </div>
  </body>
</html>

<!-- 模态框结构代码 -->
<div
  class="modal fade"
  id="myModal"
  tabindex="-1"
  role="dialog"
  aria-labelledby="myModalLabel"
  aria-hidden="true"
>
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button
          type="button"
          class="close"
          data-dismiss="modal"
          aria-hidden="true"
        >
          &times;
        </button>
        <h4 class="modal-title" id="myModalLabel">
          温馨提示
        </h4>
      </div>
      <div class="modal-body">
        在这里添加一些文本
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">
          关闭
        </button>
      </div>
    </div>
    <!-- /.modal-content -->
  </div>
  <!-- /.modal -->
</div>

<script>
  $(function () {
    // 1.点击事件
    $('.input_sub').on('click', function (e) {
      e.preventDefault()
      let username = $('.input_txt')
        .val()
        .trim()
      let password = $('.input_pass')
        .val()
        .trim()

      // 2.判断是否为空
      if (username == '' || password == '') {
        // alert('请输入完整信息')
        $('#myModal').modal()
        $('.modal-body').text('请输入完整信息')
        return
      }

      // 3.发送ajax请求
      $.ajax({
        type: 'post',
        url: 'http://localhost:8080/api/v1/admin/user/login',
        data: {
          username: username,
          password: password
        },
        success: function (backData) {
          console.log(backData)
          // alert(backData.msg)
          // 6.调用模态框
          $('#myModal').modal()
          $('.modal-body').text(backData.msg)

          // 5.阻止模态框自动执行
          $('#myModal').on('hidden.bs.modal', function () {
            // 4.成功登入
            if (backData.code === 200) {
              // 7.存储token
              localStorage.setItem('token', backData.token)
              window.location = './index.html'
            } else if (backData.code === 400) {
              // 5.失败登入
              $('.input_txt').val('')
              $('.input_pass').val('')
            }
          })
        }
      })
    })
  })
</script>
